package com.mojian.controller.points;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mojian.common.Result;
import com.mojian.entity.PointsRecord;
import com.mojian.entity.UserPoints;
import com.mojian.service.PointsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import cn.dev33.satoken.stp.StpUtil;

import java.util.List;
import java.util.Map;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/points")
@Api(tags = "门户-积分管理")
public class PointsController {

    private final PointsService pointsService;

    @GetMapping("/info")
    @SaCheckLogin
    @ApiOperation(value = "获取用户积分信息")
    public Result<UserPoints> getUserPoints(@ApiParam(value = "用户ID") @RequestParam(required = false) Long userId) {
        System.out.println("API接收到获取积分信息请求 - 用户ID参数: " + userId);
        
        // 获取当前登录用户ID
        Long loginUserId = StpUtil.getLoginIdAsLong();
        
        // 如果前端没有传用户ID或者非管理员查询其他用户，则使用当前登录用户ID
        if (userId == null || (userId != loginUserId && !StpUtil.hasRole("admin"))) {
            userId = loginUserId;
        }
        
        System.out.println("查询积分信息 - 使用用户ID: " + userId);
        
        UserPoints userPoints = pointsService.getUserPointsById(userId);
        return Result.success(userPoints);
    }

    @GetMapping("/records")
    @SaCheckLogin
    @ApiOperation(value = "获取用户积分记录")
    public Result<Page<PointsRecord>> getPointsRecords(
            @ApiParam(value = "页码", required = true) @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam(value = "每页数量", required = true) @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam(value = "用户ID") @RequestParam(required = false) Long userId) {
        
        System.out.println("API接收到积分记录请求 - 页码: " + pageNum + ", 每页数量: " + pageSize + ", 用户ID参数: " + userId);
        
        // 获取当前登录用户ID
        Long loginUserId = StpUtil.getLoginIdAsLong();
        
        // 如果前端没有传用户ID或者非管理员查询其他用户，则使用当前登录用户ID
        if (userId == null || (userId != loginUserId && !StpUtil.hasRole("admin"))) {
            userId = loginUserId;
        }
        
        System.out.println("查询积分记录 - 使用用户ID: " + userId);
        
        Page<PointsRecord> result = pointsService.getPointsRecords(pageNum, pageSize, userId);
        System.out.println("API返回积分记录 - 记录数: " + (result != null && result.getRecords() != null ? result.getRecords().size() : "null"));
        
        return Result.success(result);
    }

    @GetMapping("/debug/records")
    @ApiOperation(value = "调试: 获取积分记录表数据")
    public Result<List<Map<String, Object>>> debugPointsRecords() {
        try {
            List<Map<String, Object>> records = pointsService.debugGetAllPointsRecords();
            System.out.println("调试: 数据库中积分记录总数: " + (records != null ? records.size() : "null"));
            return Result.success(records);
        } catch (Exception e) {
            System.err.println("调试查询出错: " + e.getMessage());
            e.printStackTrace();
            return Result.error("查询出错: " + e.getMessage());
        }
    }

    @GetMapping("/debug/user-records/{userId}")
    @ApiOperation(value = "调试: 获取特定用户的积分记录")
    public Result<List<PointsRecord>> debugUserPointsRecords(@PathVariable Long userId) {
        try {
            // 从数据库直接查询该用户的积分记录
            List<PointsRecord> records = pointsService.debugGetUserPointsRecords(userId);
            System.out.println("调试: 用户ID " + userId + " 的积分记录数: " + (records != null ? records.size() : "null"));
            return Result.success(records);
        } catch (Exception e) {
            System.err.println("调试查询出错: " + e.getMessage());
            e.printStackTrace();
            return Result.error("查询出错: " + e.getMessage());
        }
    }
} 